<template>
    <div>
        <div class="sear_list">
            <div class="container">
                <div class="head">
                    <div class="iconfont" @click="$router.back()">
                        &#xe624;
                    </div>
                    <div class="sear_wrap">
                        <div class="iconfont">
                            &#xeb86;
                        </div>
                        <input type="text" placeholder="周末宅家做美食" v-model="dataId.val">
                    </div>
                    <span>搜索</span>
                </div>
                <div class="nav">
                    <div class="sort">
                        <span :class="[dataId.nav == 0 ? 'bold':'']" @click="navId(0,'all')">综合</span>
                        <span :class="[dataId.nav == 1 ? 'bold':'']" @click="navId(1,'ph')">收藏高</span>
                        <span :class="[dataId.nav == 2 ? 'bold':'']" @click="navId(2,'pw')">学做多</span>
                    </div>
                    <div class="sieve">
                        <ul>
                            <li :class="[dataId.uls == index ? 'con' : '']" 
                            v-for="(item,index) in uls" 
                            :key="index"
                            @click="ulsId(index,item)">
                                {{item}}
                            </li>
                            
                        </ul>
                    </div>
                </div>
                <div class="content">
                    <div class="list" v-for="(item,index) in list" :key="index" @click="go(item.r.id)">
                        <div class="img">
                            <img :src="item.r.p" alt="">
                        </div>
                        <div class="info">
                            <h4>{{item.r.n}}</h4>
                            <span>{{ item.r.rate }}分</span>
                            <div class="pag">
                                <div :class="[item.r.id % 2 == 0 ? 'pag_x' :'pag_y']">{{ item.r.id % 2 == 0 ? '飙升榜':'荣誉殿堂'}}</div>
                            </div>
                            <div class="outher">
                                <img :src="item.r.a.p" alt="">
                                <span>{{item.r.a.n}}</span>
                            </div>
                        </div>
                    </div>
                    
                </div>
            </div>
        </div>
    </div>
</template>

<script>
import { ref } from "vue"
import { getJsonSearch } from "../api/search.js";
import { useRoute, useRouter } from 'vue-router';
import { reactive } from '@vue/runtime-core'
import { useStore } from "../store/index";
export default {
    setup() {
        let store = useStore(); 
        let route = useRoute();
        let router = useRouter();
        let info = ref(null);
        let dataList = ref(null);
        let list = ref(null)
        let uls = ref(null);
        let dataId = reactive({
            nav:0,
            uls:0,
            val:store.search
        })

        function getdata() {//拉数据
            let id = route.query.id;
            getJsonSearch({ keyword:id }).then(data => {
                info.value = data
                console.log(info.value);
                // info.value = info.value[0];
                dataList.value = info.value.result.list;
                list.value = dataList.value;
                uls.value = info.value.result.secondary_keywords;
            })
                
        }

        function navId(id,type) {//改变排序
            
            if(type == 'all'){
                getdata()

            }else if(type == 'ph'){
                dataList.value = dataList.value.sort((a, b) => {
                    return a.r.ph - b.r.ph
                })

            }else if(type == 'pw'){
                dataList.value = dataList.value.sort((a, b) => {
                    return a.r.pw - b.r.pw
                })
            }

            dataId.nav = id
        }

        function ulsId(id,type) {//数据筛查
            dataId.uls = id
            list.value = [];
            if(type == '全部'){
                list.value = dataList.value;
                return 
            }
            dataList.value.forEach(item => {
                let bool = item.r.major.some(item => {
                    return item.title == type
                })
                if(bool == true){
                    list.value.push(item);
                }
            })
        
        }
        function go(id) {
            console.log(id);
            router.push({
                path: "/about",
                query: {
                    id
                }
            })
        }

        getdata()
        return {
            info,
            route,
            dataList,
            uls,
            dataId,
            navId,
            ulsId,
            list,
            go
            
        }
    }
    }
</script>

<style lang="scss" scoped>
.sear_list{
    .head {
            padding: 10px 16px;
            display: flex;
            justify-content: space-between;
            align-items: center;
    
            &>.iconfont {
                font-size: 20px;
                color: #000;
            }
    
            .sear_wrap {
                position: relative;
                width: 230px;
                padding-left: 30px;
                background-color: #F6F6F6;
                height: 40px;
    
                &>.iconfont {
                    position: absolute;
                    top: 10px;
                    left: 6px;
                    font-size: 20px;
                    color: #909090;
                }
    
                input {
                    border: 0px;
                    background-color: #F6F6F6;
                    width: 100%;
                    height: 100%;
                }
            }
    
            span {
                font-size: 13px;
                color: #333;
            }
    }
    .nav{
        .sort{
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 0 16px;
            span{
                color: #898989;
                font-size: 13px;
            }
            .bold{
                font-weight: bold;
                font-size: 14px;
                color: #333;
            }
        }
        .sieve{
            height: 40px;
            overflow: hidden;
            width: 100%;
            ul{
                padding-top: 10px;
                
                display: flex;
                overflow-x: scroll;
                padding-left: 16px;
                padding-bottom: 10px;
                li{
                    padding: 5px 10px;
                    border-radius: 12px;
                    background-color: #FDFDFD;
                    color: #AEAEAE;
                    font-size: 13px;
                    margin-right: 10px;
                    white-space: nowrap;
                }
                .con{
                    background-color: #FAC000;
                    color: #333;
                }
            }
        }
    }
    .content{
        padding: 16px;
        .list{
            padding-bottom: 16px;
            display: flex;
            .img{
                padding-right: 10px;
                img{
                    width: 176px;
                    height: 122px;
                    border-radius: 4px;
                    display: block;
                }
            }
            .info{
                display: flex;
                flex-direction: column;
                justify-content: center;
                h4{
                    font-size: 14px;
                    color: #333;
                    font-weight: normal;
                    padding-bottom: 8px;
                }
                span{
                    font-size: 12px;
                    color: #616161;
                    display: block;
                    padding-bottom: 8px;
                }
                .pag{
                    display: flex;
                    padding-bottom: 8px;
                    .pag_x{
                        padding: 0 3px 0 15px;
                        border-radius: 2px;
                        background-color: #4C4849;
                        border: 2px solid #333;
                        font-size: 12px;
                        color: #D4CDBE;
                        vertical-align: middle;
                        margin-right: 5px;
                        position: relative;
                        &::before{
                            content: "";
                            width: 13px;
                            height: 14px;
                            background: url('../assets/img_gu/2u.png') no-repeat;
                            background-size: 100%;
                            background-position: 0px -2px;
                            display: block;
                            position: absolute;
                            top: -2px;
                            left: -2px;

                        }
                    }
                    .pag_y{
                        
                        text-align: center;
                        padding: 3px 5px;
                        font-size: 12px;
                        background:url('../assets/img_gu/h8.png');
                        background-size: 100%;
                        color: #333;
                    }
                }
                .outher{
                    display: flex;
                    align-items: center;
                    img{
                        width: 18px;
                        height: 18px;
                        border-radius: 50%;
                    }
                    span{
                        font-size: 12px;
                        color: #CAC6C3;
                        padding:0 0 0 5px;
                    }
                }
            }
        }
    }
}
</style>